package com.wendy.common.core.json.jackson;

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;

import java.util.Collection;
import java.util.List;

/**
 * <pre>
 * 此modifier主要做的事情为：
 * 1.当序列化类型为数组集合时，当值为null时，序列化成[]
 * 2.String类型值序列化为""
 * </pre>
 * @author
 */
public class MyBeanSerializerModifier extends BeanSerializerModifier {

    @Override
    public List<BeanPropertyWriter> changeProperties(SerializationConfig config,
                                                     BeanDescription beanDesc,
                                                     List<BeanPropertyWriter> beanProperties) {
        // 循环所有的beanPropertyWriter
        for (int i = 0; i < beanProperties.size(); i++) {
            BeanPropertyWriter writer = beanProperties.get(i);
            // 判断字段的类型，如果是数组或集合则注册nullSerializer
            if (isArrayType(writer)) {
                writer.assignNullSerializer(new CustomizeNullJsonSerializer.NullArrayJsonSerializer());
                continue;
            }
            if (isArrayType(writer)) {
                writer.assignNullSerializer(new CustomizeNullJsonSerializer.NullArrayJsonSerializer());
                continue;
            }
            if (isStringType(writer)) {
                writer.assignNullSerializer(new CustomizeNullJsonSerializer.NullStringJsonSerializer());
                continue;
            }
            if (isLongType(writer)) {
                writer.assignNullSerializer(new CustomizeNullJsonSerializer.NullLongJsonSerializer());
                continue;
            }
            if (isNumberType(writer)) {
                writer.assignNullSerializer(new CustomizeNullJsonSerializer.NullNumberJsonSerializer());
                continue;
            }
            if (isBooleanType(writer)) {
                writer.assignNullSerializer(new CustomizeNullJsonSerializer.NullBooleanJsonSerializer());
                continue;
            }
            boolean isEnum = isEnum(writer);
            boolean isEnumAnnotation=isEnumAnnotation(writer);

            if (isEnum&&!isEnumAnnotation) {
                writer.assignNullSerializer(new CustomizeNullJsonSerializer.NullStringJsonSerializer());
                continue;
            }
            if (isEnum&&isEnumAnnotation) {
                writer.assignNullSerializer(new CustomizeNullJsonSerializer.NullEnumAnnJsonSerializer());
                continue;
            }
        }
        return beanProperties;
    }

    /**
     * 是否是数组
     */
    private boolean isArrayType(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getType().getRawClass();
        return clazz.isArray() || Collection.class.isAssignableFrom(clazz);
    }

    /**
     * 是否是String
     */
    private boolean isStringType(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getType().getRawClass();
        return CharSequence.class.isAssignableFrom(clazz) || Character.class.isAssignableFrom(clazz);
    }

    /**
     * 是否是数值类型
     */
    private boolean isNumberType(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getType().getRawClass();
        return Number.class.isAssignableFrom(clazz);
    }
    /**
     * 是否是Long类型
     */
    private boolean isLongType(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getType().getRawClass();
        return Long.class.isAssignableFrom(clazz);
    }

    /**
     * 是否是boolean
     */
    private boolean isBooleanType(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getType().getRawClass();
        return clazz.equals(Boolean.class);
    }

    /**
     * 是否是枚举
     * @param writer
     * @return
     */
    private boolean isEnum(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getType().getRawClass();
        return clazz.isEnum();
    }

    private boolean isEnumAnnotation(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getType().getRawClass();
        EnumSerializer enumSerializer=writer.getAnnotation(EnumSerializer.class);
        return enumSerializer != null;
    }

}
